package book.nettyinaction.ch06;

import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;

/**
 * 代码清单 6-5 修改 ChannelPipeline
 *
 * 稍后，你将看到，重组 ChannelHandler 的这种能力使我们可以用它轻松地实现极其灵活的逻辑。
 */
public class Demo05ChannelPipelineModify {

    public static void main(String[] args) {
        ChannelPipeline pipeline = null;

        // 创建一个 FirstHandler 的实例
        FirstHandler firstHandler = new FirstHandler();

        // 将该实例作为 "handler1" 添加到 ChannelPipeline 中
        pipeline.addLast("handler1", firstHandler);

        // 注意，着意味着它将被放置在已有的 "handler1" 之前
        pipeline.addFirst("handler2", new SecondHandler());

        pipeline.addLast("handler3", new ThirdHandler());

        // ...

        pipeline.remove("handler3"); // 通过名称移除"handler3"

        // 通过引用移除 FirstHandler
        pipeline.remove(firstHandler);

        // 替换
        pipeline.replace("handler2", "handler4", new ForthHandler());
    }

    private static class FirstHandler extends ChannelInboundHandlerAdapter {}

    private static class SecondHandler extends ChannelInboundHandlerAdapter {}

    private static class ThirdHandler extends ChannelInboundHandlerAdapter {}

    private static class ForthHandler extends ChannelInboundHandlerAdapter {}

}
